<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
	<title>Step Sequencer</title>

	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="icon" type="image/png" sizes="174x174" href="./favicon.png">

	<script src="https://unpkg.com/@webcomponents/webcomponentsjs@^2/webcomponents-bundle.js"></script>
	<script src="../build/Tone.js"></script>
	<script src="./js/tonejs-ui.js"></script>
	<style type="text/css">
		tone-transport {
			margin-bottom: 10px;
		}
	</style>
</head>
<body>
	<tone-example>
		<tone-loader></tone-loader>
		<tone-explanation label="Step Sequencer">
			<a href="https://tonejs.github.io/docs/Transport">Tone.Transport</a> 
			is the application-wide timekeeper. It's clock source enables sample-accurate scheduling as well as tempo-curves and automation. This example uses Tone.Sequence to invoke a callback every 16th note.
		</tone-explanation>

		<tone-content>
			<tone-transport></tone-transport>
			<tone-step-sequencer></tone-step-sequencer>
		</tone-content>
	</tone-example>


	<script type="text/javascript">
		//setup a polyphonic sampler
		var keys = new Tone.Players({
			"A" : "./audio/casio/A1.[mp3|ogg]",
			"C#" : "./audio/casio/Cs2.[mp3|ogg]",
			"E" : "./audio/casio/E2.[mp3|ogg]",
			"F#" : "./audio/casio/Fs2.[mp3|ogg]",
		}, {
			"volume" : -10,
			"fadeOut" : "64n",
		}).toMaster();

		//the notes
		var noteNames = ["F#", "E", "C#", "A"];

		var loop = new Tone.Sequence(function(time, col){
			var column = document.querySelector("tone-step-sequencer").currentColumn;
			column.forEach(function(val, i){
				if (val){
					//slightly randomized velocities
					var vel = Math.random() * 0.5 + 0.5;
					keys.get(noteNames[i]).start(time, 0, "32n", 0, vel);
				}
			});
			//set the columne on the correct draw frame
			Tone.Draw.schedule(function(){
				document.querySelector("tone-step-sequencer").setAttribute("highlight", col);
			}, time);
		}, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], "16n").start(0);

		//bind the interface
		document.querySelector("tone-transport").bind(Tone.Transport);

		Tone.Transport.on("stop", () => {
			setTimeout(() => {
				document.querySelector("tone-step-sequencer").setAttribute("highlight", "-1");
			}, 100);
		});

	</script>
</body>
</html>
